<?php
namespace app\know\controller;

use app\know\model\ChapterModel;
use app\know\controller\AdminKnowBaseController;
use app\know\validate\ChapterValidate;

/**
 * Class AdminChapterController 教材章节控制器
 * @package app\know\controller
 */
class AdminChapterController extends AdminKnowBaseController
{
    public function index()
    {
        $content = hook_one('know_admin_chapter_index_view');

        if (!empty($content))
		{
            return $content;
        }

        return $this->fetch();
    }

    public function list()
    {
        $relate_uuid = $this->request->param('relate_uuid', '');
		if ($relate_uuid == '')
		{
			$this->error('关联id不能为空');
		}
        $where = ['relate_uuid' => $relate_uuid, 'is_delete' => 0 ];

        $rows = ChapterModel::where($where)->select();
        
        $this->success('', null, $rows);
    }
 
    public function tree()
    {
        $relate_uuid = $this->request->param('relate_uuid', '');
		if ($relate_uuid == '')
		{
			$this->error('关联id不能为空');
		}
		
        $where = ['relate_uuid' => $relate_uuid, 'is_delete' => 0 ];

        $model = new ChapterModel();
		$tree = $model->getChapterTree($relate_uuid);
        
        $this->success('', null, $tree);
    }
	
	public function add()
    {
        $param = $this->request->param();

        $user = $this->getAdminUser();
        $param['create_user'] = $user['user_login'];
		
        try 
		{
			$model = new ChapterModel();
			$param['uuid'] = $model->getUUID();
            $this->validateFailError($param, 'Chapter.add');
        } 
		catch (ValidateException $e) 
		{
            $this->error($e->getError());
        }

        if ($param['parent_uuid'])
        {
            $parent = ChapterModel::where('uuid' , $param['parent_uuid'])
                                    ->where('is_delete', 0)
                                    ->find();
            if (empty($parent))
            {
                $this->error('无效上级');
            }  
            else
            {
                $param['level'] = $parent['level'] + 1;
            }                      
        }
        else
        {
            $param['level'] = 1;
        }
        
        $row = ChapterModel::create($param);
        if ($row) 
		{
            $this->opLog([
                'table_name' => 'know_chapter',
                'sql' => ChapterModel::getLastSql(),
                'create_user' => $user['user_login'],
            ]);
            $this->success('添加成功');
        }
        $this->error('添加失败');  
    }
	
	public function delete()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try 
		{
            $this->validateFailError($param, 'Chapter.delete');
        } 
		catch (ValidateException $e) 
		{
            $this->error($e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try 
		{
            ChapterModel::where('id', 'in', preg_split("/[\s;,]/", $id))->save(['is_delete' => 1]);
            $this->opLog([
                'table_name' => 'know_chapter',
                'sql' => ChapterModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($id),
            ]);
        } 
		catch (\Throwable $th) 
		{
            $this->error($th->getMessage());
        }
        $this->success('已删除');
    }
	
	public function edit()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try 
		{
            $this->validateFailError($param, 'Chapter.edit');
        } 
		catch (ValidateException $e) 
		{
            $this->result('', 0, $e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try 
		{
            $d = ChapterModel::find($id);
            ChapterModel::where('id', $id)->save($param);
            $this->opLog([
                'table_name' => 'know_chapter',
                'sql' => ChapterModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($d),
            ]);
        } 
		catch (\Throwable $th) 
		{
            $this->error($th->getMessage());
        }
        $this->success('修改成功');
    }
	
	public function maxOrder()
    {
        $order = ChapterModel::max('order_number');
        $this->success('', '', $order);
    }

    public function getItem()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        if (empty($id))
        {
            $this->error('章节id不能为空');
        }

        $w = ['id' => $id, 'is_delete' => 0];
        $d = ChapterModel::where($w)->find();

        $this->success('', null, $d);
    }
	

}
